﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace CinemabookingWCFApp.BookingFolder
{
    public class BookingService : IBookingService
    {
        #region
        CinemabookingDBDataContext db = new CinemabookingDBDataContext();
        #endregion
        
        public List<Booking> GetAllBookings()
        {
            var queryResult = (from x in db.Bookings
                               select x);
            return queryResult.ToList();
        }

         public List<Booking> GetBooking(int BookId)
        {
            var queryResult = (from x in db.Bookings
                               where x.id == BookId
                               select x);
            return queryResult.ToList();
        }

        public List<Booking> GetBookingOnPhone(string BookPhone)
         {
             var queryResult = (from x in db.Bookings
                                where x.cusPhoneNr == BookPhone
                                select x);
             return queryResult.ToList();
         }

        public void SaveBooking(string CusName, string CusPhone)
         {
             Booking newBook = new Booking { cusName = CusName, cusPhoneNr = CusPhone };

             newBook.cusName = CusName;
             newBook.cusPhoneNr = CusPhone;

             db.Bookings.InsertOnSubmit(newBook);
             db.SubmitChanges();

         }

        public void DeleteBooking(string CusPhone)
        {
            Booking delBook = (from x in db.Bookings
                               where x.cusPhoneNr == CusPhone
                               select x).FirstOrDefault();
            if (delBook !=null)
            {
                db.Bookings.DeleteOnSubmit(delBook);
                db.SubmitChanges();
            }
        }

        public void UpdateBooking(int BookId, string CusName, string CusPhone )
        {
            Booking updBook = (from x in db.Bookings
                               where x.id == BookId
                               select x).Single();

            updBook.cusName = CusName;
            updBook.cusPhoneNr = CusPhone;

            db.SubmitChanges();
        }

    }
}
